/*
* 1videoConference -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;

using _videoConference.BAL;

namespace _videoConference
{
    /// <summary>
    /// Interaction logic for ManageMeeting.xaml
    /// </summary>

    public partial class ManageMeeting : System.Windows.Controls.UserControl,IDisposable
    {
        DataSet ds = new DataSet();
        DataSet dsacc = new DataSet();
        Label lMeetingId, lAccId, lMeetingName, lMDate, lMStartTime, lMEndTime, lFName;
        Button btnEdit, btnDelete,btnCancel;
        Label CaptionMName = new Label();
        Label CaptionMDate = new Label();
        Label CaptionMStartTime = new Label();
        Label CaptionMEndTime = new Label();
        Label CaptionMMange = new Label();
        Label CaptionHName = new Label();
        //Line ln;
        IAddPage ia = new IAddPage();
        private bool disposed = false;

        public ManageMeeting()
        {
            InitializeComponent();
            myInkC.EditingMode = InkCanvasEditingMode.None;
            CaptionHName.Foreground = Brushes.White;
            CaptionMDate.Foreground = Brushes.White;
            CaptionMEndTime.Foreground = Brushes.White;
            CaptionMMange.Foreground = Brushes.White;
            CaptionMName.Foreground = Brushes.White;
            CaptionMStartTime.Foreground = Brushes.White;
            //ln = new Line();

            //ln.X1 = 10.0;
            //ln.Y1 = 33.0;
            //ln.X2 = myInkC.Width - 70.0;
            //ln.Y2 = 33.0;

            //ln.Fill = Brushes.Black;
            //ln.Stroke = Brushes.Black;
            //ln.StrokeThickness = 0.8;


            CaptionMName.Content = "Meeting Name";
            CaptionMDate.Content = "Meeting Date";
            CaptionMStartTime.Content = "Meeting Start time";
            CaptionMEndTime.Content = "Meeting End time";
            CaptionMMange.Content = "Manage Meeting";
            CaptionHName.Content = "Name";

            myInkC.Children.Add(CaptionMName);
            myInkC.Children.Add(CaptionMDate);
            myInkC.Children.Add(CaptionMStartTime);
            myInkC.Children.Add(CaptionMEndTime);
            myInkC.Children.Add(CaptionMMange);
            myInkC.Children.Add(CaptionHName);
            //myInkC.Children.Add(ln);

            CaptionMName.FontWeight = FontWeights.Bold;
            CaptionMDate.FontWeight = FontWeights.Bold;
            CaptionMStartTime.FontWeight = FontWeights.Bold;
            CaptionMEndTime.FontWeight = FontWeights.Bold;
            CaptionMMange.FontWeight = FontWeights.Bold;
            CaptionHName.FontWeight = FontWeights.Bold;


            CaptionMName.FontSize = 12.0;
            CaptionMDate.FontSize = 12.0;
            CaptionMStartTime.FontSize = 12.0;
            CaptionMEndTime.FontSize = 12.0;
            CaptionMMange.FontSize = 12.0;
            CaptionHName.FontSize = 12.0;

            CaptionMName.SetValue(InkCanvas.TopProperty, 10.0);
            CaptionMDate.SetValue(InkCanvas.TopProperty, 10.0);
            CaptionMStartTime.SetValue(InkCanvas.TopProperty, 10.0);
            CaptionMEndTime.SetValue(InkCanvas.TopProperty, 10.0);
            CaptionMMange.SetValue(InkCanvas.TopProperty, 10.0);
            CaptionHName.SetValue(InkCanvas.TopProperty, 10.0);

            CaptionMName.SetValue(InkCanvas.LeftProperty, 10.0);
            CaptionHName.SetValue(InkCanvas.LeftProperty, 150.0);


            CaptionMDate.SetValue(InkCanvas.LeftProperty, 260.0);
            CaptionMStartTime.SetValue(InkCanvas.LeftProperty, 380.0);
            CaptionMEndTime.SetValue(InkCanvas.LeftProperty, 540.0);

            CaptionMMange.SetValue(InkCanvas.LeftProperty, 690.0);

            LinearGradientBrush l;
            GradientStop gs1;
            GradientStop gs2;

            l = new LinearGradientBrush();

            Point p1 = new Point(0.0, 0.5);
            Point p2 = new Point(1.0, 0.5);

            l.StartPoint = p1;
            l.EndPoint = p2;

            gs2 = new GradientStop(System.Windows.Media.Color.FromRgb(89, 89, 89), 0.11428571428571427);
            gs1 = new GradientStop(System.Windows.Media.Color.FromRgb(115, 115, 115), 0.51428571428571427);

            System.Windows.Media.RotateTransform RT;
            RT = new RotateTransform(90.0);

            l.Transform = RT;
            l.GradientStops.Add(gs1);
            l.GradientStops.Add(gs2);

            ds = ia.getmeetinginfo();

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    dsacc = ia.getuserinfo(ds.Tables[0].Rows[i][1].ToString());

                    if (dsacc.Tables.Count > 0 && dsacc.Tables[0].Rows.Count > 0)
                    {
                        lFName = new Label();
                        lFName.Foreground = Brushes.White;
                        lFName.Content = dsacc.Tables[0].Rows[0][1].ToString();

                    }
                    lMeetingId = new Label();
                    lMeetingId.Foreground = Brushes.White;
                    lAccId = new Label();
                    lAccId.Foreground = Brushes.White;
                    lMeetingName = new Label();
                    lMeetingName.Foreground = Brushes.White;
                    lMDate = new Label();
                    lMDate.Foreground = Brushes.White;
                    lMStartTime = new Label();
                    lMStartTime.Foreground = Brushes.White;
                    lMEndTime = new Label();
                    lMEndTime.Foreground = Brushes.White;
                    btnEdit = new Button();
                    btnDelete = new Button();
                   
                    btnEdit.Click += new RoutedEventHandler(btnEdit_Click);
                    btnDelete.Click += new RoutedEventHandler(btnDelete_Click);
                   
                    btnEdit.Content = "Edit";
                    btnDelete.Content = "Delete";
                    

                    btnEdit.Width = 60.0;
                    btnDelete.Width = 80.0;

                    btnDelete.Background = l;
                    btnDelete.Foreground = Brushes.White;

                    btnEdit.Background = l;
                    btnEdit.Foreground = Brushes.White;


                    lFName.Tag = ds.Tables[0].Rows[i][0].ToString();
                    lFName.FontSize = 14.0;

                    lMeetingId.Content = ds.Tables[0].Rows[i][0].ToString();
                    lMeetingId.Tag = ds.Tables[0].Rows[i][0].ToString();


                    lAccId.Content = ds.Tables[0].Rows[i][1].ToString();
                    lAccId.Tag = ds.Tables[0].Rows[i][0].ToString();

                    lMeetingName.Content = ds.Tables[0].Rows[i][2].ToString();
                    lMeetingName.Tag = ds.Tables[0].Rows[i][0].ToString();
                    lMeetingName.FontSize = 14.0;


                    lMDate.Content = ds.Tables[0].Rows[i][3].ToString();
                    lMDate.Tag = ds.Tables[0].Rows[i][0].ToString();
                    lMDate.FontSize = 14.0;


                    lMStartTime.Content = ds.Tables[0].Rows[i][4].ToString();
                    lMStartTime.Tag = ds.Tables[0].Rows[i][0].ToString();
                    lMStartTime.FontSize = 14.0;


                    lMEndTime.Content = ds.Tables[0].Rows[i][5].ToString();
                    lMEndTime.Tag = ds.Tables[0].Rows[i][0].ToString();
                    lMEndTime.FontSize = 14.0;


                    btnEdit.Tag = ds.Tables[0].Rows[i][0].ToString();
                    btnDelete.Tag = ds.Tables[0].Rows[i][0].ToString();

                    
                    lMeetingName.SetValue(InkCanvas.LeftProperty, 10.0);

                    lFName.SetValue(InkCanvas.LeftProperty, 150.0);

                    lMDate.SetValue(InkCanvas.LeftProperty, 260.0);
                    lMStartTime.SetValue(InkCanvas.LeftProperty, 380.0);
                    lMEndTime.SetValue(InkCanvas.LeftProperty, 550.0);

                    btnEdit.SetValue(InkCanvas.LeftProperty, 700.0);
                    btnDelete.SetValue(InkCanvas.LeftProperty, 770.0);

                    lMeetingName.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);
                    lFName.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);
                    lMDate.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);
                    lMStartTime.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);
                    lMEndTime.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);

                    btnEdit.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);
                    btnDelete.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);


                    myInkC.Children.Add(lMeetingName);
                    myInkC.Children.Add(lFName);
                    myInkC.Children.Add(lMDate);
                    myInkC.Children.Add(lMStartTime);
                    myInkC.Children.Add(lMEndTime);
                    myInkC.Children.Add(btnEdit);
                    myInkC.Children.Add(btnDelete);
                }
            }

            btnCancel = new Button();
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            btnCancel.Content = "Cancel";
            btnCancel.Width = 80.0;
            btnCancel.Height = 25.0;
            btnCancel.SetValue(InkCanvas.LeftProperty,390.0);
            btnCancel.SetValue(InkCanvas.TopProperty, double.Parse(((Button)myInkC.Children[myInkC.Children.Count - 1]).GetValue(InkCanvas.TopProperty).ToString())+60.0);
            myInkC.Children.Add(btnCancel);            

            btnCancel.Background = l;
            btnCancel.Foreground = Brushes.White;

          
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvMain")).IsEnabled = true;
            ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).IsEnabled = true;
            ((InkCanvas)this.Parent).Children.Remove(this);
        }

        void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Are you sure that you want to delete this meeting?", "Manage Meeting", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                string strdate = "";
                string strstarttime = "";
                string strendtime = "";
                for (int i = 0; i < myInkC.Children.Count; i++)
                {
                    if (myInkC.Children[i].GetValue(InkCanvas.LeftProperty).ToString() == "260" && myInkC.Children[i].GetValue(InkCanvas.TopProperty).ToString() != "10")
                    {
                        //Date
                        if (((Label)myInkC.Children[i]).Tag.ToString() == ((Button)sender).Tag.ToString())
                        {
                            strdate = ((Label)(myInkC.Children[i])).Content.ToString();
                        }
                    }
                    if (myInkC.Children[i].GetValue(InkCanvas.LeftProperty).ToString() == "380" && myInkC.Children[i].GetValue(InkCanvas.TopProperty).ToString() != "10")
                    {
                        //Start T
                        if (((Label)myInkC.Children[i]).Tag.ToString() == ((Button)sender).Tag.ToString())
                        {
                            strstarttime = ((Label)(myInkC.Children[i])).Content.ToString();
                        }
                    }
                    if (myInkC.Children[i].GetValue(InkCanvas.LeftProperty).ToString() == "550" && myInkC.Children[i].GetValue(InkCanvas.TopProperty).ToString() != "10")
                    {
                        //End T
                        if (((Label)myInkC.Children[i]).Tag.ToString() == ((Button)sender).Tag.ToString())
                        {
                            strendtime = ((Label)(myInkC.Children[i])).Content.ToString();
                        }
                    }
                }

                bool bool1 = ia.deletemeetinginfo(strdate, strstarttime, strendtime);
                if (bool1)
                {
                    int j = 0;
                    while (j < myInkC.Children.Count)
                    {
                        try
                        {
                            if (myInkC.Children[j].GetType() == typeof(Label) && ((Label)myInkC.Children[j]).Tag != null && ((Label)myInkC.Children[j]).Tag.ToString() == ((Button)sender).Tag.ToString())
                            {
                                myInkC.Children.Remove(myInkC.Children[j]);
                            }
                            else if (myInkC.Children[j].GetType() == typeof(Button) && ((Button)myInkC.Children[j]).Tag != null && ((Button)myInkC.Children[j]).Tag.ToString() == ((Button)sender).Tag.ToString())
                            {
                                myInkC.Children.Remove(myInkC.Children[j]);
                            }
                            else
                            {
                                j++;
                            }
                        }
                        catch (Exception exp)
                        {
                            exp.Data.Add("My Key", "1videoConference--:--ManageMeeting.xaml.cs--:--btnDelete_Click()--:--");
                            clsUtility.LogException = exp;
                            break;                          
                        }
                    }
                }

                ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvMain")).IsEnabled = true;
                ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).IsEnabled = true;
                ((InkCanvas)this.Parent).Children.Remove(this);
            }
        }

        void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            UserInfo.crtmeeting = ((Button)sender).Tag.ToString();
            CreateMeeting cm = new CreateMeeting();
            ((InkCanvas)this.Parent).Children.Add(cm);
            ((InkCanvas)this.Parent).Children.Remove(this); ;
        }

         public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);    
        }

         private void Dispose(bool disposing)
        {
            if(!this.disposed)
            {
                if(disposing)
                { 
                   
                }        
                disposed = true;

            }
        }

        ~ManageMeeting()
        {
            Dispose(false);
        }
    }
}